package com.axonactive.yolo.service.implement;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.axonactive.yolo.dao.interfaces.FlightNotificationRepository;
import com.axonactive.yolo.domain.FlightNotification;
import com.axonactive.yolo.dto.FlightNotificationDTO;
import com.axonactive.yolo.exception.FlightNotificationNotFoundException;
import com.axonactive.yolo.service.interfaces.FlightNotificationService;
@PropertySource("classpath:messages.properties")
@Service
public class FlightNotificationServiceImpl implements FlightNotificationService{
	@Resource
	private Environment env;

	@Resource
	private FlightNotificationRepository flightNotificationRepository;

	private static final String FLIGHT_NOTIFICATION_EXCEPTION = "flight.notification.notFound";
	private static final Logger LOGGER = Logger.getLogger(FlightNotificationServiceImpl.class);


	@Override
	@Transactional
//	@CacheEvict(value = "listFlightNotificationByUserIdCache", beforeInvocation = true)
//	@Cacheable(value="listFlightNotificationByUserIdCache", key="#userId")
	public List<FlightNotification> findByUserId(int userId) {
		return flightNotificationRepository.findByUserId(userId);
	}
	
	@Override
	@Transactional
//	@CacheEvict(value = "listFlightNotificationByFlightIdCache", beforeInvocation = true)
//	@Cacheable(value="listFlightNotificationByFlightIdCache", key="#flightId")
	public List<FlightNotification> findByFlightId(String flightId) {
		return flightNotificationRepository.findByFlightId(flightId);
	}

	@Override
	@Transactional
	public FlightNotification update(FlightNotification flightNotification) throws FlightNotificationNotFoundException {
		FlightNotification updateflightNotification = flightNotificationRepository.findOne(flightNotification.getId());

		if(updateflightNotification == null){
			//throw new FlightNotificationNotFoundException(env.getRequiredProperty(FLIGHT_NOTIFICATION_EXCEPTION));
			LOGGER.info("TEST NULL");
			return null;
		}

		updateflightNotification.setId(flightNotification.getId());
		updateflightNotification.setFlightId(flightNotification.getFlightId());
		updateflightNotification.setFlightDepartureTime(flightNotification.getFlightDepartureTime());
		updateflightNotification.setFlightRoute(flightNotification.getFlightRoute());
		updateflightNotification.setFlightChangeType(flightNotification.getFlightChangeType());
		updateflightNotification.setNewPrice(flightNotification.getNewPrice());
		updateflightNotification.setOldPrice(flightNotification.getOldPrice());
		updateflightNotification.setUserId(flightNotification.getUserId());
		updateflightNotification.setStatus(flightNotification.getStatus());
		updateflightNotification.setIsRead(flightNotification.getIsRead());

		return flightNotificationRepository.save(updateflightNotification);
	}

	@Override
	public FlightNotification create(FlightNotification flightNotification) {
		FlightNotification createFlightNotification = flightNotification;
		return flightNotificationRepository.save(createFlightNotification);
	}

	@Override
//	@CacheEvict(value = "listFlightNotificationByUserStatusIdCache", beforeInvocation = true)
//	@Cacheable(value="listFlightNotificationByUserStatusIdCache", key="")
	public List<FlightNotification> findByUserIdAndStatus(int userId, int status) {
		return flightNotificationRepository.findByUserIdAndStatus(userId, status);
	}

	@Override
	public List<FlightNotificationDTO> convertToListFlightNotificationDTO(
			List<FlightNotification> flightNotifications) {
		List<FlightNotificationDTO> flightNotificationDTOs =new ArrayList<FlightNotificationDTO>();
		FlightNotificationDTO flightNotificationDTO = null;

		for (int i = 0; i < flightNotifications.size(); i++) {
			flightNotificationDTO = new FlightNotificationDTO();
			flightNotificationDTO.setId(flightNotifications.get(i).getId());
			flightNotificationDTO.setUserId(flightNotifications.get(i).getUserId());
			flightNotificationDTO.setFlightId(flightNotifications.get(i).getFlightId());
			flightNotificationDTO.setFlightRoute(flightNotifications.get(i).getFlightRoute());
			flightNotificationDTO.setFlightDepartureTime(flightNotifications.get(i).getFlightDepartureTime());
			flightNotificationDTO.setOldPrice(flightNotifications.get(i).getOldPrice());
			flightNotificationDTO.setNewPrice(flightNotifications.get(i).getNewPrice());
			flightNotificationDTO.setCurrencyCode(flightNotifications.get(i).getCurrencyCode());
			flightNotificationDTO.setFlightChangeType(flightNotifications.get(i).getFlightChangeType());
			flightNotificationDTO.setStatus(flightNotifications.get(i).getStatus());
			flightNotificationDTO.setSaveTime(flightNotifications.get(i).getSaveTime());
			flightNotificationDTO.setDriftTime(flightNotifications.get(i).getSaveTime());
			flightNotificationDTO.setIsRead(flightNotifications.get(i).getIsRead());
			flightNotificationDTOs.add(flightNotificationDTO);
		}

		return flightNotificationDTOs;
	}

	@Override
	@Transactional(rollbackFor = FlightNotificationNotFoundException.class)
	public boolean delete(int id) throws FlightNotificationNotFoundException {
		FlightNotification flightNotification = findById(id);
		try {
			flightNotificationRepository.delete(flightNotification);
		} catch (Exception e) {
			throw new FlightNotificationNotFoundException(FLIGHT_NOTIFICATION_EXCEPTION);
		}
		return true;
	}

	@Override
	@Transactional
//	@CacheEvict(value = "listFlightNotificationByIdCache", beforeInvocation = true)
//	@Cacheable(value="listFlightNotificationByIdCache", key="#id")
	public FlightNotification findById(int id) throws FlightNotificationNotFoundException{
		FlightNotification flightNotification = new FlightNotification();
		try {
			flightNotification = flightNotificationRepository.findOne(id);
		} catch (Exception e) {
			throw new FlightNotificationNotFoundException(FLIGHT_NOTIFICATION_EXCEPTION);
		}
		return flightNotification;
	}

	@Override
	public boolean deleteAllByUserId(int userId)
			throws FlightNotificationNotFoundException {
		List<FlightNotification> listFlightNotification = new ArrayList<FlightNotification>();
		listFlightNotification = findByUserId(userId);
		try {
			for(int i=0; i<listFlightNotification.size();i++){
				flightNotificationRepository.delete(listFlightNotification.get(i));
			}
		} catch (Exception e) {
			throw new FlightNotificationNotFoundException(FLIGHT_NOTIFICATION_EXCEPTION);
		}
		return true;
	}

	@Override
	public boolean deleteAllByFlightId(String flightId)
			throws FlightNotificationNotFoundException {
		List<FlightNotification> listFlightNotification = new ArrayList<FlightNotification>();
		listFlightNotification = findByFlightId(flightId);
		try {
			for(int i=0; i<listFlightNotification.size();i++){
				flightNotificationRepository.delete(listFlightNotification.get(i));
			}
		} catch (Exception e) {
			throw new FlightNotificationNotFoundException(FLIGHT_NOTIFICATION_EXCEPTION);
		}
		return true;
	}

}
